package com.tanhua.server.service;

import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 13:23  2021/10/18
 */
@Service
public class UserSetService {

    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;

    public SettingsVo findById() {
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setId(UserHolder.getUserId());
        settingsVo.setPhone(UserHolder.getMobile());
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        settingsVo.setStrangerQuestion(question == null ? "你喜欢我吗?" : question.getTxt());
        Settings settings = settingsApi.findByUserId(UserHolder.getUserId());
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    public void setQuestion(String content) {
        long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        if (question == null) {
            question = new Question();
            question.setUserId(UserHolder.getUserId());
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.setQuest(question);
        }
    }

    public void setSetting(Map map) {
        Boolean likeNotification = (Boolean)map.get("likeNotification");
        Boolean pinglunNotification = (Boolean)map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean)map.get("gonggaoNotification");
        Settings settings = settingsApi.findByUserId(UserHolder.getUserId());
        if (settings == null) {
            settings=new Settings();
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        }else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }
}
